import {
    getBreadCrumbList,
    setTagNavListInLocalstorage,
    getMenuByRouter,
    getTagNavListFromLocalstorage,
    getHomeRoute,
    routeHasExist,
} from '@/libs/util'
import routers from '@/router/routers'

export default {
    state: {
        breadCrumbList: [],
        tagNavList: [],
        homeRoute: getHomeRoute(routers),
        local: '',
    },
    getters: {
        menuList(state, getters, rootState) {
            var user = localStorage.getItem('user_admin')
            if (user && JSON.parse(user).type == 1) {
                user = JSON.parse(user)
                let routerlink = routers.filter(item => {
                    return item.meta.user_type == user.type
                })
                return getMenuByRouter(routerlink, rootState.user.access)
            } else {
                let routerlink = routers.filter(item => {
                    return item.meta.user_type !== 1
                })
                return getMenuByRouter(routerlink, rootState.user.access)
            }
        }
    },
    mutations: {
        setBreadCrumb(state, routeMetched) {
            state.breadCrumbList = getBreadCrumbList(routeMetched, state.homeRoute)
        },
        setTagNavList(state, list) {
            if (list) {
                state.tagNavList = [...list]
                setTagNavListInLocalstorage([...list])
            } else {
                state.tagNavList = getTagNavListFromLocalstorage()
            }
        },
        addTag(state, {
            route,
            type = 'unshift'
        }) {
            if (!routeHasExist(state.tagNavList, route)) {
                if (type === 'push') {
                    state.tagNavList.push(route)
                } else {
                    
                }
                setTagNavListInLocalstorage([...state.tagNavList])
            }
        },
        setLocal(state, lang) {
            state.local = lang
        }
    }
}
